syslib.ui.Accordeon = {};
syslib.ui.Accordeon.Conversor = syslib.object.createClass();
syslib.ui.Accordeon.Conversor.extend(syslib.ui.VisualTag);
syslib.ui.Accordeon.Conversor.extend({
	id : "", 
	itemSize : 0, 
	height : 0,
	menuHeight: 0, 
	itens : [], 
	selectedItem : null, 

	prepare : function(){
		// load the style
		syslib.loadStyleForComponent("accordeon");

		// get the tag attributes
		var attributes = this.__attributes;

		// set the class attributes
		this.id = (__valid(attributes.id)) ? attributes.id : syslib.element.generateID();
		this.itemSize = 0;
		this.height = 0;
		this.menuHeight = 0;
		this.itens = [];
		this.selectedItem = null;
		
		// return
		return true;
	},

	execute : function(){
		// format the attributes
		var attributes = syslib.object.extend(this.__attributes, { 
			"id" : this.id,
			"class" : "syslib-accordeon-main-container", 
			"innerName" : "container"
		});

		// create the structure
		var elemtJSON = ["div", attributes];
		

		// create the element
		var accordeon = this.createDOM(elemtJSON);

		// update the inner html
		this.__refs.container.innerHTML = this.__innerHTML;

		// return
		return accordeon;
	},

	finalize : function(){
	},

	onEnterBody : function(){
		// if no item is selected, select the first 
		if(!__valid(this.selectedItem)){
			this.selectItem(0);
		}
	
		// schedule the size correction to run after one second
		setTimeout(this.setSize.applyTo(this), 1000);
		
		// set the events
		syslib.event.listen("resize", window, function(){
			// schedule the size correction to run after one seconds
			setTimeout(this.setSize.applyTo(this), 1000);
		}.applyTo(this));
	},

	setSize : function(){
		// get the parent height
		var parentHeight = this.__element.parentNode.clientHeight;
		this.height = parentHeight - (this.__element.clientTop * 2) - 2;
		
		// get the content height
		var contentHeight = parentHeight - this.height;
		if(contentHeight < 0){ 
			return;
		}
		
		// set the style
		this.__element.style.overflow = "hidden";
		
		// set the content container height
		if(syslib.ui.fx.isLoaded(syslib.ui.fx.Resize)){
			syslib.ui.fx.Resize.setHeight(this.__element, this.height);
		} else {
			this.__element.style.height = this.height + "px";
		}
		
		// get the selected item
		var selectedItem = this.itens[this.selectedItem];
		
		// set the selected item height
		if(syslib.ui.fx.isLoaded(syslib.ui.fx.Resize)){
			syslib.ui.fx.Resize.setHeight(selectedItem.body, (this.height - this.menuHeight));
		} else {
			selectedItem.body.style.height = (this.height - this.menuHeight) + "px";
		}
	},
	addItem : function(item){
		// get the index
		var index = this.itens.length;
		
		// set the item
		this.itens[index] = item; 
		
		// set the height
		this.menuHeight += 32;
		
		// return
		return index;
	},

	selectItem : function(id){
		// check if there's a selected item
		if(__valid(this.selectedItem)){
			// get the selected item
			var selectedItem = this.itens[this.selectedItem];
			
			// update the status of the item
			selectedItem.controller.__unselect();
			
			// resize
			if(syslib.ui.fx.isLoaded(syslib.ui.fx.Resize)){
				syslib.ui.fx.Resize.setHeight(selectedItem.body, 0);
			} else {
				selectedItem.body.style.height = "0px";				
			}
		}
		
		// ge tthe item to be selected
		var item = this.itens[id];
		
		// update the status of the item
		item.controller.__select();
		
		// set this item as the selected item of this accordeon
		this.selectedItem = id;
		
		// calculates the new size
		var newSize = this.height - this.menuHeight;
		if(newSize <= 0){
			return;
		}
		
		// resize
		if(syslib.ui.fx.isLoaded(syslib.ui.fx.Resize)){
			syslib.ui.fx.Resize.setHeight(item.body, newSize);
		} else {
			item.body.style.height = newSize + "px";  				
		}
	}
});